class DateUtil {

    static checkAddZone(num) {
        return num < 10 ? `0${num.toString()}` : num
    }

    static dateFormatInit(value, fmt = 'yyyy-mm-dd HH:MM:SS') {
        let date = new Date(value)
        let fmtDate = fmt
        //转化时间
        let o = {
            "y+": date.getFullYear(),
            "m+": date.getMonth() + 1,
            "d+": date.getDate(),
            "H+": date.getHours(),
            "M+": date.getMinutes(),
            "S+": date.getSeconds()
        }
        for (let key in o) {
            // 正则
            let rep = new RegExp(`${key}`);
            // 是所需要的格式
            if (rep.test(fmt)) {
                // 有匹配到所对应格式
                fmtDate = fmtDate.match(rep) ? fmtDate.replace(
                    fmt.match(rep)[0], // 将原来的格式进行值的替换 例如 yyyy - 2019
                    String(o[key]).padStart(fmt.match(rep)[0].length, "0") // ES2017写法
                    // String(o[key].length === 1 ? `0${o[key]}` : `${o[key]}`) // 例 小时为 7 时，在前面加上 0 ，变成 07
                ) : "";
            }
        }
        return fmtDate;
    }

    static filterTime(nows) {
        if (!nows) return ''
        const now = new Date(nows)
        const year = now.getFullYear()

        let month = now.getMonth() + 1
        month = this.checkAddZone(month)

        let date = now.getDate()
        date = this.checkAddZone(date)
        return `${year}-${month}-${date}`
    }

    static filterTimeYmdHms(t) {
        if (!t) return ''
        t = new Date(t).getTime() // eslint-disable-line
        t = new Date(t) // eslint-disable-line
        const year = t.getFullYear()
        let month = t.getMonth() + 1
        month = this.checkAddZone(month)

        let date = t.getDate()
        date = this.checkAddZone(date)

        let hour = t.getHours()
        hour = this.checkAddZone(hour)

        let min = t.getMinutes()
        min = this.checkAddZone(min)

        let se = t.getSeconds()
        se = this.checkAddZone(se)

        return `${year}-${month}-${date} ${hour}:${min}:${se}`
    }

    static filterTimeYear(value) {
        // 过滤时间戳, 返回值 今年:mm-dd 往年:yyyy-mm-dd
        const jy = 1900 + new Date().getYear()
        const date = new Date(value)
        const y = 1900 + date.getYear()
        const m = `0${date.getMonth() + 1}`
        const d = `0${date.getDate()}`
        const val = `${y}-${m.substring(m.length - 2, m.length)}-${d.substring(d.length - 2, d.length)}`
        const thisYear = `${m.substring(m.length - 2, m.length)}-${d.substring(d.length - 2, d.length)}`
        if (jy === y) {
            return thisYear
        }
        return val
    }

    static filterTimeHour(value) {
        return this.dateFormatInit(value, 'H')
    }

    static addHour(value, hours = 1) {
        let tempDate = ''
        if (value instanceof Date) {
            tempDate = value;
        } else if (typeof value == 'string') {
            tempDate = new Date(value.replace(/-/g, '/'));
        } else {
            throw "gaddHour方法参数错误！";
        }
        const now = new Date(tempDate)
        return new Date(now.setHours(now.getHours() + hours))
    }

    static addDay(value, days = 1) {
        let tempDate = ''
        if (value instanceof Date) {
            tempDate = value;
        } else if (typeof value == 'string') {
            tempDate = new Date(value.replace(/-/g, '/'));
        } else {
            throw "addDay方法参数错误！";
        }
        const now = new Date(tempDate)
        return new Date(now.setHours(now.getHours() + days * 24))
    }




}

export default DateUtil